// AWTColumnLayout.cpp: implementation of the AWTColumnLayout class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWTColumnLayout.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTColumnLayout, AWTLayout)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWTColumnLayout::AWTColumnLayout()
{
	m_gap = 5;
}

AWTColumnLayout::AWTColumnLayout(int gap)
{
	m_gap = gap;
}

AWTColumnLayout::~AWTColumnLayout()
{

}

void AWTColumnLayout::addLayoutComponent(CString* name, AWTComponent* comp)
{
}

void AWTColumnLayout::removeLayoutComponent(AWTComponent* comp)
{
}

CSize AWTColumnLayout::preferredLayoutSize(AWTContainer* target)
{
  AWTInsets insets = target->getInsets();
  CSize dim(0,0);
  int ncomponents = target->getComponentCount();
  AWTComponent* comp;
  CSize d(0,0);

  for (int i = 0 ; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
    if(comp->isVisible()) 
		{
			d = comp->getPreferredSize();
			if(i > 0) 
				dim.cy += m_gap;
      dim.cy += d.cy;
      dim.cx = max(d.cx, dim.cx);
    }
  }
  dim.cx  += insets.left + insets.right;
  dim.cy += insets.top  + insets.bottom;
  return dim;
}

CSize AWTColumnLayout::minimumLayoutSize(AWTContainer* target)
{
	AWTInsets insets = target->getInsets();
	CSize dim(0,0);
	int ncomponents = target->getComponentCount();
	AWTComponent* comp;
	CSize d;

	for (int i = 0 ; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
		if(comp->isVisible()) 
		{
			d = comp->getMinimumSize();
			dim.cx = max(d.cx, dim.cx);
			dim.cy += d.cy;
			if(i > 0) dim.cy += m_gap;
		}
	}
	dim.cx  += insets.left + insets.right;
	dim.cy += insets.top  + insets.bottom;
	return dim;
}

void AWTColumnLayout::layoutContainer(AWTContainer* target)
{
  AWTInsets insets = target->getInsets();
  int top = insets.top;
  int left = 0;
  int ncomponents   = target->getComponentCount();
  CSize preferredSize = target->getSize();
  CSize targetSize = target->getSize();
  AWTComponent* comp;
  CSize ps;

//  top += (targetSize.cy/2) - (preferredSize.cy/2);

  for (int i = 0 ; i < ncomponents ; i++) 
	{
    comp = target->getComponent(i);
    left = insets.left;
    if(comp->isVisible()) 
		{
			ps = comp->getPreferredSize();
//			left = (targetSize.cx/2) - (ps.cx/2);
      comp->setBounds(left,top,ps.cx,ps.cy);
      top += ps.cy + m_gap;
    }
  }
}
